﻿using PagedList;
using PagedList.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GiaoTrinhEPU.Repositories.Repositories;
using GiaoTrinhEPU.Repositories.DataModels;
using GiaoTrinhEPU.Repositories;
using GiaoTrinhEPU.Repositories.Interfaces;
using GiaoTrinhEPU.Core.ActionResults;
using GiaoTrinhEPU.Core;

namespace GiaoTrinhEPU.Controllers
{
    public class SyllabusController : BaseController
    {
        /// <summary>
        /// Declare Syllabus Repository
        /// </summary>
        private SyllabusRepository syllabusRepository;

        /// <summary>
        ///  Declare Session
        /// </summary>
        public static string SessionSyllabusSort = "SessionSyllabusSort";
        public static string SessionSyllabusSearch = "SessionSyllabusSearch";
        public static string SessionSyllabusSortOpt = "SessionSyllabusSortOpt";



        // GET: /Syllabus/
        /// <summary>
        /// Constructor that allow to create a Comment controller
        /// </summary>
        public SyllabusController()
            : base()
        {
            this.syllabusRepository = new SyllabusRepository();
            ViewBag.SelectedMenuId = 25;
        }

        public ActionResult Index(int page = 1, int size = 25, String search = "", String opt = "", String sort = "")
        {
            ViewBag.Title = "Giáo Trình";
            // Stored Session

            Session[SessionSyllabusSort] = sort.Trim().Length > 0 ? sort.ToLower() : Session[SessionSyllabusSort];
            Session[SessionSyllabusSortOpt] = opt.Trim().Length > 0 ? opt.ToLower() : Session[SessionSyllabusSortOpt];

            var syllabus = this.syllabusRepository.ListSyllabus(false).ToList();

            // Search
            if (Session[SessionSyllabusSearch] != null)
            {
                ViewBag.Keyword = Session[SessionSyllabusSearch].ToString();

                syllabus = syllabus.Where(l => l.Name.ToUpper().Contains(ViewBag.Keyword.ToUpper())).ToList();

            }
            // Sort 
            ViewBag.SortNameOpt = "desc";
            if (Session[SessionSyllabusSort] != null)
            {
                switch (Session[SessionSyllabusSort].ToString())
                {
                    case "name":
                        syllabus = Session[SessionSyllabusSortOpt].ToString().Equals("desc") ? syllabus.OrderByDescending(s => s.Name).ToList() : syllabus.OrderBy(s => s.Name).ToList();

                        Session[SessionSyllabusSort] = "name";
                        ViewBag.SortNameOpt = Sort.SetSortOrderOption(Session[SessionSyllabusSortOpt].ToString());
                        break;

                    default:
                        break;
                }
            }


            return View(syllabus.ToPagedList(page, size));
        }
 

}  

    }

       